
  $(document).ready(function(){
	 $.ajax({
		 type:"post",
		 url:"showMail.action",
		 success:function(data){
			 var json = $.parseJSON(data);
			 var result = $.parseJSON(json);
			 var allrow = result[0];
			 var pageSize = 8;
			 var count = parseInt(allrow % pageSize == 0 ? allrow / pageSize : allrow / pageSize + 1);
			// var pageindex = 1;
			 var container = document.getElementById("MailPage");
			// setPage(container, count, pageindex);
			 var mailData = result[1];
			 var itableHtml = '';
			 for(var data in mailData){
				 itableHtml += 
						'<tr style="cursor: pointer;width:60%" onclick=\"showMail('+mailData[data].id+')\">'+
							'<td>'+
								mailData[data].email+
							'</td>'+
							'<td>'+
								mailData[data].status+
							'</td>'+
							'<td>'+
								mailData[data].date+
							'</td>'+
							'<td>'+
								'<a href="javascript:void(0)" onclick=\"delMail('+mailData[data].id+')\">删除</a>'+
								//'<a href="javascript:void(0)">回复</a>'+
							'</td>';
			 }
			 $("#MailTable").html(itableHtml);
			 goMailPage(container, count,1);
		 }
	 });
 });
  
  function goMailPage(container,count,pageindex){
	  	 var pageSize = 8;
	  	 
		 var itable = document.getElementById("MailTable");
		 var num = itable.rows.length;//表格行数
		 
		 var startRow = (pageindex - 1) * pageSize;//开始显示的行数
		 var endRow = pageindex * pageSize;//结束显示的行数 
		 
		 endRow = (endRow > num)? num : endRow;
		 
//		 for(var i=0;i<1;i++){
//				var irow = itable.rows[i];
//				irow.style.display = "";
//			}
			
			for(var i=0;i<num;i++){
				var irow = itable.rows[i];
				if(i>=startRow&&i<endRow){
					irow.style.display = "";	
				}else{
					irow.style.display = "none";
				}
			}
			var a = [];
		    //总页数少于10 全部显示,大于10 显示前3 后3 中间3 其余....
		    if (pageindex == 1) {
		      a[a.length] = "<li><a href=\"#\">上一页</a></li>";
		    } else {
		      a[a.length] = "<li><a href=\"#\" onclick=\"goMailPage("+(pageindex-1)+")\">上一页</a></li>";
		    }
		    function setPageList(i) {
		      if (pageindex == i) {
		        a[a.length] = "<li><a href=\"#\" class=\"on\" onclick=\"goMailPage("+ i +")\">" + i + "</a></li>";
		      } else {
		        a[a.length] = "<li><a href=\"#\" onclick=\"goMailPage("+i+")\">" + i + "</a></li>";
		      }
		    }
		    //总页数小于10
		    if (count <= 10) {
		      for (var i = 1; i <= count; i++) {
		        setPageList(i);
		      }
		    }
		    //总页数大于10页
		    else {
		      if (pageindex <= 4) {
		        for (var i = 1; i <= 5; i++) {
		          setPageList(i);
		        }
		      } else if (pageindex >= count - 3) {
		        for (var i = count - 4; i <= count; i++) {
		          setPageList(i);
		        }
		      }
		      else { //当前页在中间部分
		        for (var i = pageindex - 2; i <= pageindex + 2; i++) {
		          setPageList(i);
		        }
		      }
		    }
		    if (pageindex == count) {
		      a[a.length] = "<li><a href=\"#\">下一页</a></li>";
		    } else {
		      a[a.length] = "<li><a href=\"#\" onclick=\"goMailPage("+(pageindex+1)+")\">下一页</a><li>";
		    }
		    container.innerHTML = a.join("");
		    
		    var oAlink = container.getElementsByTagName("a");
		      var inx = pageindex; //初始的页码
		      oAlink[0].onclick = function() { //点击上一页
		        if (inx == 1) {
		          return false;
		        }
		        inx--;
		        goMailPage(container, count, inx);
		        return false;
		      };
		      for (var i = 1; i < oAlink.length - 1; i++) { //点击页码
		        oAlink[i].onclick = function() {
		          inx = parseInt(this.innerHTML);
		          goMailPage(container, count, inx);
		          return false;
		        };
		      }
		      oAlink[oAlink.length - 1].onclick = function() { //点击下一页
		        if (inx == count) {
		          return false;
		        }
		        inx++;
		        goMailPage(container, count, inx);
		        return false;
		      };
  }
  
  	function delMail(id){
  		$.ajax({
  			url:'delMail.action',
  			data:'mail.id='+id,
  			success:function(data){
  				if(data=="\"del\""){
  					alert("删除成功");
  					window.location.reload();
  				}else{
  					alert("由于未知原因删除失败");
  					window.location.reload();
  				}
  			}
  		});
  	}
  	
  	function showMail(id){
  		$.ajax({
  			url:'showMailOne.action',
  			data:'mail.id='+id,
  			success:function(data){
  				var json = $.parseJSON(data);
  				var mail = $.parseJSON(json);
  				var mailHtml = 
  					'<div class="container" style="margin-top: 10px;">'+
  						'<div class="row">'+
  							'<div class="span12" style="margin-bottom:10px;">'+
  								//'<div>'+
  									'邮箱：'+mail.email+'<a href="javascript:void(0)" style="float:right;" onclick=\"sendMail('+id+',\''+mail.email+'\')\">回复</a><br/>'+
  									'时间：'+mail.date+'<a href="javascript:void(0)" style="float:right;" onclick=\"window.location.reload()\">返回</a><br/>'+
  								//'</div>'+
  							'</div>'+
  							'<div class="span12" style="border: 2px dashed gray;border-left:none;border-right:none;padding:5px;">'+
  								'<pre>'+mail.content+'</pre>'+
  							'</div>'+
  						'</div>'+
  					'</div>'
  					;
  				$("#tab7").html(mailHtml);
  			}
  		});
  	}
  	
  	function sendMail(id,email){
  		var emailHtml = 
  		'<div class="container">'+
  			'发送至：'+email+'<br/>'+
  			'<input type="text" value="请输入标题" id="Subject" style="height: 40px;width: 100%"/>'+
  			'<textarea rows="30" cols="50" name="editor02" id="newContent">请输入内容</textarea>'+
  			'<script type="text/javascript">var editor=CKEDITOR.replace(\'editor02\');</script>'+
  			'<div style="float: right;margin-top: 10px"><a href="javascript:void(0)" onclick=\"send('+id+',\''+email+'\')\">发送</a>&nbsp;&nbsp<a href="javascript:void(0)" onclick="window.location.reload();">返回</a></div>'+
  		'</div>';
  		$("#tab7").html(emailHtml);
  	}
  	
  	function send(id,email){
  		if($("#Subject").val().trim()==null||$("#Subject").val().trim()==""){
  			alert("请输入标题");
  			$("#Subject").val("请输入标题");
  			return;
  		}
  		if(editor.getData().trim()==null||editor.getData().trim()==""){
  			alert("请输入内容");
  			editor.setData("请输入内容");
  			return;
  		}
  		var Subject = $("#Subject").val().trim();
  		var mail = editor.getData();
  		
  		$.ajax({
  			type:'post',
  			url:'sendMail.action',
  			data:'mailVo.subject='+Subject+'&mailVo.content='+mail+'&mailVo.id='+id+'&mailVo.email='+email,
  			success:function(data){
  				if(data=="\"sended\""){
  					alert("邮件已发送");
  					window.location.reload();
  				}else{
  					alert("由于未知原因邮件发送失败");
  					window.location.reload();
  				}
  			}
  		});
  	}